package com.simon.annual.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import com.simon.annual.entity.ReportEmployee;
import com.simon.annual.entity.ReportManagement;
import com.simon.annual.entity.ReportNeeq;
import com.simon.annual.repositoy.IReportEmployeeRepositoy;
import com.simon.annual.repositoy.IReportManagementRepositoy;
import com.simon.annual.service.ICompanyPublicInfo;

/**
 * 第八节 董事、监事、高级管理人员及员工情况
 * */
@Service
public class ManagementImpl{

	@Resource
	private ICompanyPublicInfo cmpanyPublicInfo;
	@Resource
	private IReportManagementRepositoy reportManagementRepositoy;
	@Resource
	private IReportEmployeeRepositoy reportEmployeeRepositoy;
	
	public ReportNeeq analysis(Element e, ReportNeeq reportNeeq, int companyPtrId) {
		//基本情况
		Element management = cmpanyPublicInfo.findNextTag("table", e);
		Elements managementTr = management.select("table tr");
		List<ReportManagement> reportManagementList = new ArrayList<ReportManagement>();
		for (int i = 1; i < managementTr.size()-3; i++) {
			Elements managementTd = managementTr.get(i).children();
			ReportManagement reportManagement = new ReportManagement();
			reportManagement.setName(managementTd.get(0).text());
			reportManagement.setTitle(managementTd.get(1).text());
			reportManagement.setGender(managementTd.get(2).text());
			reportManagement.setAge(managementTd.get(3).text());
			reportManagement.setEduBackground(managementTd.get(4).text());
			reportManagement.setTermOfService(managementTd.get(5).text());
			reportManagement.setIsPaidSalary(managementTd.get(6).text());
			reportManagement.setProjectScopeId(companyPtrId);
			reportManagementList.add(reportManagement);
		}
		//董事会人数
		Elements boardMember = managementTr.get(managementTr.size()-3).children();
		reportNeeq.setBoardMemberNo(StringUtil.isBlank(boardMember.last().text())?boardMember.get(0).text().replaceAll("\\D+", ""):boardMember.last().text());
		Elements supervisor = managementTr.get(managementTr.size()-2).children();
		reportNeeq.setSupervisorMeetingNo(StringUtil.isBlank(supervisor.last().text())?supervisor.get(0).text().replaceAll("\\D+", ""):supervisor.last().text());
		Elements senior = managementTr.get(managementTr.size()-1).children();
		reportNeeq.setSeniorManagerNo(StringUtil.isBlank(senior.last().text())?senior.get(0).text().replaceAll("\\D+", ""):senior.last().text());
		//董事、监事、高级管理人员相互间关系及与控股股东、实际控制人间关系
		Element relationship = cmpanyPublicInfo.findNextTag("table", management);
		reportNeeq.setRelationshipOfManagers(relationship.text());
		//持股情况
		Element  stockManagement  = cmpanyPublicInfo.findNextTag("table", relationship);
		Elements trs = stockManagement.select("table tr");
		for (int i = 1; i < trs.size()-1; i++) {
			Elements managementTd = trs.get(i).children();
			ReportManagement reportManagement = new ReportManagement();
			reportManagement.setName(managementTd.get(0).text());
			reportManagement.setTitle(managementTd.get(1).text());
			reportManagement.setBgnCommonShares(managementTd.get(2).text());
			reportManagement.setCommonSharesChange(managementTd.get(3).text());
			reportManagement.setEndCommonShares(managementTd.get(4).text());
			reportManagement.setEndCommonSharePct(managementTd.get(5).text());
			reportManagement.setEndShareOption(managementTd.get(6).text());
			reportManagement.setProjectScopeId(companyPtrId);
			reportManagementList.add(reportManagement);
		}
		reportManagementRepositoy.save(reportManagementList);
		//（三）变动情况
		Element  changed  = cmpanyPublicInfo.findNextTag("table", stockManagement);
		Elements changedTd = changed.select("table tr > *");
		reportNeeq.setIsChairmanChanged(changedTd.get(2).text());
		reportNeeq.setIsGeneralManagerChanged(changedTd.get(4).text());
		reportNeeq.setIsBoardSecretaryChanged(changedTd.get(6).text());
		reportNeeq.setIsFinancialControllerChanged(changedTd.get(8).text());
		//本年新任董事、监事、高级管理人员简要职业经历
		Element  experience  = cmpanyPublicInfo.findNextTag("table", changed);
		reportNeeq.setWorkExperienceOfSeniorManagers(experience.text());
		//员工情况按工作性质分类
		Element employeeDep = experience.nextElementSibling().nextElementSibling().nextElementSibling();
		Elements employeeDepTr = employeeDep.select("table tr");
		List<ReportEmployee> reportEmployeeList = new ArrayList<ReportEmployee>();
		for (int i = 1; i < employeeDepTr.size()-1; i++) {
			Elements employeeDepTd = employeeDepTr.get(i).children();
			ReportEmployee reportEmployee = new ReportEmployee();
			reportEmployee.setGroup(employeeDepTd.get(0).text());
			reportEmployee.setBgnNo(employeeDepTd.get(1).text());
			reportEmployee.setEndNo(employeeDepTd.get(2).text());
			reportEmployee.setProjectScopeId(companyPtrId);
			reportEmployee.setGroupBy("dep");
			reportEmployeeList.add(reportEmployee);
		}
		//员工情况按教育程度分类
		Element employeeEdu = employeeDep.nextElementSibling().tagName().startsWith("table")
				?employeeDep.nextElementSibling():cmpanyPublicInfo.findNextTag("table", employeeDep);
		Elements employeeEduTr = employeeEdu.select("table tr");
		for (int i = 1; i < employeeEduTr.size()-1; i++) {
			Elements employeeEduTd = employeeEduTr.get(i).children();
			ReportEmployee reportEmployee = new ReportEmployee();
			reportEmployee.setGroup(employeeEduTd.get(0).text());
			reportEmployee.setBgnNo(employeeEduTd.get(1).text());
			reportEmployee.setEndNo(employeeEduTd.get(2).text());
			reportEmployee.setProjectScopeId(companyPtrId);
			reportEmployee.setGroupBy("edu");
			reportEmployeeList.add(reportEmployee);
		}
		reportEmployeeRepositoy.save(reportEmployeeList);
		//人员变动、人才引进、培训、招聘、薪酬政策、需公司承担费用的离退休职工人数等情况
		Element  hrReview  = cmpanyPublicInfo.findNextTag("table", employeeEdu);
		reportNeeq.setHrReview(hrReview.text());
		//核心员工
		Element  employeeCore  = cmpanyPublicInfo.findNextTag("table", hrReview);
		Elements employeeCoreTr = employeeCore.select("table tr");
		for (int i = 1; i < employeeCoreTr.size(); i++) {
			Elements employeeCoreTd = employeeCoreTr.get(i).children();
			ReportEmployee reportEmployee = new ReportEmployee();
			reportEmployee.setGroup(employeeCoreTd.get(0).text());
			reportEmployee.setBgnNo(employeeCoreTd.get(1).text());
			reportEmployee.setEndNo(employeeCoreTd.get(2).text());
			reportEmployee.setEndCommonShares(employeeCoreTd.get(3).text());
			reportEmployee.setEndCommonOption(employeeCoreTd.get(4).text());
			reportEmployee.setProjectScopeId(companyPtrId);
			reportEmployee.setGroupBy("core");
			reportEmployeeList.add(reportEmployee);
		}
		reportEmployeeRepositoy.save(reportEmployeeList);
		//核心技术团队或关键技术人员的基本情况及变动情况
		Element  coreEmployeeReview  = cmpanyPublicInfo.findNextTag("table", employeeCore);
		reportNeeq.setCoreEmployeeReview(coreEmployeeReview.text());
		return reportNeeq;
	}
}
